_.extend.moveLeft   A
last analyzed

Complexity

Conditions 1
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
import _ from 'underscore';
0 ignored issues
show
introduced by
Definition for rule 'keyword-spacing' was not found
Loading history...
2
/**
3
   Command translates a DOM Event into commands that Backgrid
4
   recognizes. Interested parties can listen on selected Backgrid events that
5
   come with an instance of this class and act on the commands.
6
7
   It is also possible to globally rebind the keyboard shortcuts by replacing
8
   the methods in this class' prototype.
9
10
   @class Backgrid.Command
11
   @constructor
12
 */
13
var Command = function (evt) {
14
  _.extend(this, {
15
    altKey: !!evt.altKey,
16
    "char": evt["char"],
0 ignored issues
show
Coding Style Compatibility introduced by
As per coding-style, please use dot notation .char instead of brackets ["char"].

The dot notation is easier to read, less verbose, and also works better with certain Javascript optimizers.

Loading history...
17
    charCode: evt.charCode,
18
    ctrlKey: !!evt.ctrlKey,
19
    key: evt.key,
20
    keyCode: evt.keyCode,
21
    locale: evt.locale,
22
    location: evt.location,
23
    metaKey: !!evt.metaKey,
24
    repeat: !!evt.repeat,
25
    shiftKey: !!evt.shiftKey,
26
    which: evt.which
27
  });
28
};
29
30
_.extend(Command.prototype, {
31
  /**
32
     Up Arrow
33
34
     @member Backgrid.Command
35
   */
36
  moveUp: function () {
37
    return this.keyCode == 38;
0 ignored issues
show
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
38
  },
39
  /**
40
     Down Arrow
41
42
     @member Backgrid.Command
43
   */
44
  moveDown: function () {
45
    return this.keyCode === 40;
46
  },
47
  /**
48
     Shift Tab
49
50
     @member Backgrid.Command
51
   */
52
  moveLeft: function () {
53
    return this.shiftKey && this.keyCode === 9;
54
  },
55
  /**
56
     Tab
57
58
     @member Backgrid.Command
59
   */
60
  moveRight: function () {
61
    return !this.shiftKey && this.keyCode === 9;
62
  },
63
  /**
64
     Enter
65
66
     @member Backgrid.Command
67
   */
68
  save: function () {
69
    return this.keyCode === 13;
70
  },
71
  /**
72
     Esc
73
74
     @member Backgrid.Command
75
   */
76
  cancel: function () {
77
    return this.keyCode === 27;
78
  },
79
  /**
80
     None of the above.
81
82
     @member Backgrid.Command
83
   */
84
  passThru: function () {
85
    return !(this.moveUp() || this.moveDown() || this.moveLeft() ||
86
      this.moveRight() || this.save() || this.cancel());
87
  }
88
});
89
90
export {
91
  Command
92
};
93